package org.spring.bse.home.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.spring.bse.home.model.Stock;
import org.spring.bse.home.util.DBUtil;

public class BseIndiaDAO
{

	public List<Integer> getActiveScripCodes()
	{
		String qryString = "SELECT scripCode FROM stocks";
		List<Integer> scripCodeList = null;

		List<Object[]> resultList = DBUtil.getResultSet(qryString);

		if (resultList != null & !resultList.isEmpty())
		{
			scripCodeList = new ArrayList<Integer>(resultList.size());
			for (Object[] objArr : resultList)
			{
				scripCodeList.add((Integer) objArr[0]);
			}
		}

		return scripCodeList;
	}

	public String persistGoogleXMLFeed(List<Stock> stkList)
	{
		String str = "";

		Connection dbConnection = DBUtil.getConnection();
		PreparedStatement preparedStatement = null;

		String updateTableSQL = "UPDATE stocks SET scripCode=?, scripName=?, curValue=?, chgValue=?, chgPer=?, updatedAt=?,"
				+ " lastOpen=?, dayLow=?, dayHigh=?, lastTradeTime=?, curVolume=?, marketCap=?, lastClose=?, lastUpdateTS=? WHERE scripCode=?";

		try
		{
			preparedStatement = dbConnection.prepareStatement(updateTableSQL);
			dbConnection.setAutoCommit(false);

			for (Stock stock : stkList)
			{
				preparedStatement.setInt(1, stock.getScripCode());
				preparedStatement.setString(2, stock.getScripName());
				preparedStatement.setFloat(3, stock.getCurValue());
				preparedStatement.setFloat(4, stock.getChgValue());
				preparedStatement.setFloat(5, stock.getChgPer());
				preparedStatement.setString(6, stock.getUpdatedAt());
				preparedStatement.setFloat(7, stock.getLastOpen());
				preparedStatement.setFloat(8, stock.getDayLow());
				preparedStatement.setFloat(9, stock.getDayHigh());
				preparedStatement.setString(10, stock.getLastTradeTime().toString());
				preparedStatement.setDouble(11, stock.getCurVolume());
				preparedStatement.setDouble(12, stock.getMarketCap());
				preparedStatement.setFloat(13, stock.getLastClose());
				preparedStatement.setString(14, stock.currentTimestamp().toString());
				preparedStatement.setInt(15, stock.getScripCode());
				preparedStatement.addBatch();
			}
			preparedStatement.executeBatch();
			dbConnection.commit();
			dbConnection.close();
		}
		catch (SQLException e)
		{
			if ("database is locked".equals(e.getMessage()))
				str = "DB_LOCKED";
		}

		return str;
	}

	public List<Stock> getAllStocks()
	{
		String qryString = "SELECT scripCode, scripName, bseGroup, faceValue, curValue, chgValue, chgPer, curVolume, lastOpen, lastClose, dayLow, dayHigh, marketCap, industry, updatedAt FROM stocks ORDER BY chgPer ";
		List<Stock> stockList = null;

		List<Object> resultList = DBUtil.getResultSet(qryString, Stock.class);
		stockList = new ArrayList<Stock>(resultList.size());
		int ind=0;
		for (Object obj : resultList)
		{
			//if(ind++<50)
			stockList.add((Stock) obj);
		}

		return stockList;
	}
}
